//
//  MineCartCell.m
//  copyrightTrading
//
//  Created by 郭子一 on 2020/10/6.
//  Copyright © 2020 郭子一. All rights reserved.
//

#import "MineCartCell.h"

@interface MineCartCell ()
@property (nonatomic, strong) UIImageView *img;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *middleLab;
@property (nonatomic, strong) UILabel *priceLab;
@property (nonatomic, strong) UIImageView *selectImg;
@property (nonatomic, strong) UIButton *selectBtn;
@end

@implementation MineCartCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = RGB_VALUE(0xF5F5F5);
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
//    self.selectImg = [[UIImageView alloc] init];
//    self.selectImg.image = [UIImage imageNamed:@"MineCartCircle"];
//    [self.contentView addSubview:self.selectImg];
//    [self.selectImg mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.offset(0);
//        make.left.offset(27);
//        make.width.mas_equalTo(24);
//        make.height.mas_equalTo(24);
//    }];
    
    self.selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.selectBtn setImage:[UIImage imageNamed:@"MineCartCircle"] forState:UIControlStateNormal];
    [self.selectBtn setImage:[UIImage imageNamed:@"MineCartSelect"] forState:UIControlStateSelected];
    [self.selectBtn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.selectBtn setHitEdgeInsets:UIEdgeInsetsMake(-20, -20, -20, -13)];
    [self.contentView addSubview:self.selectBtn];
    [self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.left.offset(27);
        make.width.mas_equalTo(24);
        make.height.mas_equalTo(24);
    }];
    
    self.img = [[UIImageView alloc] init];
    self.img.layer.masksToBounds = YES;
    self.img.layer.cornerRadius = 10;
    self.img.backgroundColor = UIColor.cyanColor;
    [self.contentView addSubview:self.img];
    [self.img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.left.offset(67);
        make.width.mas_equalTo(140);
        make.height.mas_equalTo(90);
    }];
    
    self.titleLab = [UILabel new];
    self.titleLab.text = @"汽车高清宣传片";
    self.titleLab.font = JSS_MEDIUM_FONT(17);
    [self.contentView addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.img.mas_top);
        make.left.equalTo(self.img.mas_right).offset(13);
        make.right.offset(-27);
        make.height.mas_equalTo(24);
    }];
    
    self.middleLab = [UILabel new];
    self.middleLab.text = @"购买时间：2020-09-20\n类别：视频商务\n版权：个人";
    self.middleLab.font = JSS_MEDIUM_FONT(10);
    self.middleLab.textColor = RGB_VALUE(0x848484);
    self.middleLab.numberOfLines = 0;
    [self.contentView addSubview:self.middleLab];
    [self.middleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLab.mas_bottom).offset(1);
        make.left.equalTo(self.titleLab.mas_left);
        make.right.offset(-27);
        make.height.mas_equalTo(42);
    }];
    
    self.priceLab = [UILabel new];
    self.priceLab.text = @"39.00元";
    self.priceLab.font = JSS_MEDIUM_FONT(17);
    self.priceLab.textColor = RGB_VALUE(0x848484);
    [self.contentView addSubview:self.priceLab];
    [self.priceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.middleLab.mas_bottom).offset(2);
        make.left.equalTo(self.titleLab.mas_left);
        make.right.offset(-27);
        make.height.mas_equalTo(24);
    }];
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = RGB_VALUE(0xDBDBDB);
    [self.contentView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(22);
        make.right.offset(-22);
        make.height.mas_equalTo(1);
        make.bottom.offset(0);
    }];
}

- (void)setIsSelected:(BOOL)isSelected
{
    _isSelected = isSelected;
    if (isSelected) {
        self.selectImg.image = [UIImage imageNamed:@"MineCartSelect"];
        self.selectBtn.selected = YES;
    } else {
        self.selectImg.image = [UIImage imageNamed:@"MineCartCircle"];
        self.selectBtn.selected = NO;
    }
}

- (void)selectBtnClick:(UIButton *)sender
{
    sender.selected = !sender.selected;
    if ([self.delegate respondsToSelector:@selector(MineCartCellTapSelectBtn:)]) {
        [self.delegate MineCartCellTapSelectBtn:self.cellIndexPath];
    }
}

- (void)setDataDic:(NSDictionary *)dataDic
{
    _dataDic = dataDic;
    [self.img m_setImageWithUrlString:dataDic[@"thumb"] placeholder:@"placeholderImg"];
    self.titleLab.text = dataDic[@"title"];
    self.middleLab.text = [NSString stringWithFormat:@"购买时间：%@\n类别：%@\n版权：%@",dataDic[@"create_at"],dataDic[@"typename"],dataDic[@"create_at"]];
    NSString *priceString = [NSString stringWithFormat:@"%@元",dataDic[@"price"]];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:priceString];
    [string addAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:255/255.0 green:118/255.0 blue:0/255.0 alpha:1.0]} range:NSMakeRange(0, [dataDic[@"price"] length])];
    [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:132/255.0 green:132/255.0 blue:132/255.0 alpha:1.0]} range:NSMakeRange([dataDic[@"price"] length], 1)];
    self.priceLab.attributedText = string;
}
@end
